﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using SobrietyEngine.Actor;
using SobrietyEngine.Components.Graphic;

using ProjectMercury;
using ProjectMercury.Emitters;
using ProjectMercury.Modifiers;
using ProjectMercury.Renderers;


namespace SobrietyEngine.Components.Graphic.Particles
{
    public class MercuryParticleBase:Base3DGraphic
    {
        ParticleEffect pe;
        QuadRenderer qr;
        //BillboardRenderer qr;
        IPosition pos;

        public MercuryParticleBase()
            : base()
        {
        }

        public override bool InitComponent()
        {
            base.InitComponent();
            pos = Actor.Get<IPosition>();

            
            
            pe = new ParticleEffect
            {
                Emitters = new EmitterCollection
                                                         {
                                                             new PointEmitter
                                                                 {                                                                     
                                                                     Enabled = true,
                                                                     Budget = 3000,
                                                                     Term = 0.75f,
                                                                     BlendMode = EmitterBlendMode.Alpha ,
                                                                     ReleaseColour = Color.SteelBlue.ToVector3(),
                                                                     ReleaseOpacity = 1,
                                                                     ReleaseQuantity = 60,
                                                                     ReleaseRotation = new RotationRange
                                                                                           {
                                                                                               Roll =
                                                                                                   new Range(
                                                                                                   -MathHelper.Pi,
                                                                                                   MathHelper.Pi),
                                                                                           },
                                                                     ReleaseScale = new Range(0.1f, 0.5f),
                                                                     ReleaseSpeed = 12f,
                                                                     Modifiers = new ModifierCollection
                                                                                     {
                                                                                         new OpacityFastFadeModifier
                                                                                             {InitialOpacity = 0.25f},
                                                                                         new ColourInterpolator2
                                                                                             {
                                                                                                 InitialColour =
                                                                                                     Color.Orange.
                                                                                                     ToVector3(),
                                                                                                 FinalColour =
                                                                                                     Color.Yellow.
                                                                                                     ToVector3()                                                                                                    
                                                                                             },
                                                                                         new RotationModifier
                                                                                             {
                                                                                                 RotationRate =
                                                                                                     new Vector3
                                                                                                         {
                                                                                                             X = 0f,
                                                                                                             Y = 1f,
                                                                                                             Z = 0f
                                                                                                         }
                                                                                             },
                                                                                     },
                                                                 },
                                                         },
            };


            SobrietyEngine.Assets.IAssetGameService asg = Actor.GameServices().AssetGameService;
            //pe = asg.Load<ParticleEffect>("Particles/Campfire");       
            pe.Emitters[0].ParticleTexture = asg.Load<Texture2D>("Particles/star");


            pe.Emitters[0].BillboardStyle = BillboardStyle.None;

            foreach (AbstractEmitter ae in pe.Emitters)
                ae.Initialise();
            
            qr = new QuadRenderer
            {
                GraphicsDeviceService = (IGraphicsDeviceService)Actor.Game.Services.GetService(typeof(IGraphicsDeviceService)) ,
            };

            qr.LoadContent(asg.GetContentManager("SEGameExample"));
            
            return true;
        }

        Matrix world = Matrix.Identity;
        Vector3 posVec;
        float triggerTime = 50;
        float nextTrigger = 50;
        public override void UpdateGraphic(GameTime ElapsedTime)
        {
            
            posVec = pos.PositionV3;            
            world = Matrix.CreateTranslation(posVec);

            nextTrigger-= (float)ElapsedTime.ElapsedGameTime.Milliseconds;
            if (nextTrigger < 0)
            {
                Vector3 z = Vector3.Zero;
                pe.Trigger((float)ElapsedTime.ElapsedGameTime.Milliseconds, ref z);
                nextTrigger = triggerTime;
            }
            pe.Update((float)ElapsedTime.ElapsedGameTime.TotalSeconds );
        }

        Vector3 camPos;        
        public override void Draw(IActor cameraActor, Matrix projection, Matrix view)
        {
            
            camPos = cameraActor.Get<IPosition>().PositionV3;
            //Matrix m = Matrix.Identity;
            qr.RenderEffect(pe, ref world, ref view, ref projection, ref camPos);
            
        }
    }
}
